<template>
  <div>
    <el-col :span="24">
      <el-col :span="6">
        <el-input v-model="newJobLevel.name" placeholder="+添加职称"></el-input>
      </el-col>
      <el-col :span="6">
        <el-select v-model="titleLevel">
          <el-option
            :label="item"
            :value="item"
            v-for="item in titleLevelArr"
            :key="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-button :span="4" type="primary" @click="addJobLevel()"
          >+添加</el-button
        >
      </el-col>
    </el-col>
    <el-table
      ref="multipleTable"
      :data="jobLevels"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="60"></el-table-column>
      <el-table-column prop="id" label="编号" width="60"></el-table-column>
      <el-table-column
        prop="name"
        label="职称名称"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="titleLevel"
        label="职称级别"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="createDate"
        label="创建时间"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="enabled"
        label="是否启用"
        width="100"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="dialogJobLevel(scope.row)"
            >编辑</el-button
          >
          <el-popconfirm
            :title="'此操作将永久删除，确定删除[' + scope.row.name + ']吗？'"
            @onConfirm="deleteJobLevel(scope.row.id)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑职称下拉框 -->
    <el-dialog title="修改职称" :visible.sync="dialogFormJobLevel">
      <el-form :model="upJobLevel">
        <el-form-item label="职称名称" >
          <el-input v-model="upJobLevel.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="职称级别" >
          <el-select v-model="titleLevel">
            <el-option
              :label="item"
              :value="item"
              v-for="item in titleLevelArr"
              :key="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用" >
          <el-switch v-model="value1" active-text="启用" inactive-text="禁用">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormJobLevel = false">取 消</el-button>
        <el-button type="primary" @click="updateJobLevel()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "JobLevel",
  data() {
    return {
      value1: true,
      jobLevels: [],
      upJobLevel: {
        id: "",
        name: "",
        titleLevel: "",
        enabled: "",
      },
      newJobLevel: {
        name: "",
        titleLevel: "",
      },
      titleLevel: "",
      titleLevelArr: ["正高级", "副高级", "中级", "初级", "员级"],
      width: "120px",

      dialogFormJobLevel: false,
    };
  },
  created() {
    this.getAllJobLwvel();
  },
  methods: {
    getAllJobLwvel() {
      this.$axios
        .post("basic/JobLevelController/getAllJobLevel", this.$qs.stringify({}))
        .then((value) => {
          for (let i = 0; i < value.data.length; i++) {
            if (value.data[i].enabled == "1") {
              value.data[i].enabled = "已启用";
            } else {
              value.data[i].enabled = "未启用";
            }
          }
          this.jobLevels = value.data;
        })
        .catch((value) => {
          console.error(value);
        });
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    dialogJobLevel(row) {
      this.dialogFormJobLevel = true;
      this.upJobLevel.id = row.id;
      this.upJobLevel.name = row.name;
      this.titleLevel = row.titleLevel;
      if (row.enabled == "未启用") {
        this.value1 = false;
      } else {
        this.value1 = true;
      }
    },
    // 添加职称
    addJobLevel() {
      this.newJobLevel.titleLevel = this.titleLevel;
      this.$axios
        .post(
          "basic/JobLevelController/addJobLevel",
          this.$qs.stringify(this.newJobLevel)
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
          } else {
            alert("添加失败");
          }
          this.newJobLevel = { name: "", titleLevel: "" };
          this.getAllJobLwvel();
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 修改职称
    updateJobLevel() {
      this.dialogFormJobLevel = false;
      if (this.value1 == false) {
        this.upJobLevel.enabled = 0;
      } else {
        this.upJobLevel.enabled = 1;
      }
      this.upJobLevel.titleLevel = this.titleLevel;
      this.$axios
        .post(
          "basic/JobLevelController/updateJobLevel",
          this.$qs.stringify(this.upJobLevel)
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "更新成功",
              type: "success",
            });
          } else {
            alert("更新失败");
          }
          this.getAllJobLwvel();
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 删除职称
    deleteJobLevel(id) {
      this.$axios
        .post(
          "basic/JobLevelController/deleteJobLevel",
          this.$qs.stringify({ id: id })
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
          } else {
            alert("删除失败");
          }
          this.getAllJobLwvel();
        })
        .catch((value) => {
          if (value != null) {
            this.$message({
              message: "该数据含有关联数据，删除失败",
              type: "error",
            });
          }
        });
    },
  },
};
</script>